package main

import (
	"context"
	"encoding/json"
	"fmt"
	"net/http"

	"github.com/chebyrash/promise"
)

func main() {
	p1 := promise.New(func(resolve func(int), reject func(error)) {
		factorial := findFactorial(20)
		resolve(factorial)
	})
	p2 := promise.New(func(resolve func(string), reject func(error)) {
		ip, err := fetchIP()
		if err != nil {
			reject(err)
		} else {
			resolve(ip)
		}
	})

	factorial, _ := p1.Await(context.Background())
	fmt.Println(*factorial)

	IP, _ := p2.Await(context.Background())
	fmt.Println(*IP)

	//ctx := context.Background()
	//
	//// fetches data from API, runs on ioOptimizedPool
	//dataPromise := promise.NewWithPool(func(resolve func(string), reject func(error)) {
	//	data, err := fetchDataFromAPI()
	//	if err != nil {
	//		reject(err)
	//	} else {
	//		resolve(data)
	//	}
	//}, ioOptimizedPool)
	//
	//// computes result based on the fetched data, runs on cpuOptimizedPool
	//resultPromise := promise.ThenWithPool(dataPromise, ctx, func(data string) (string, error) {
	//	result, err := computeResult(data)
	//	return result, err
	//}, cpuOptimizedPool)
}

func findFactorial(n int) int {
	if n == 1 {
		return 1
	}
	return n * findFactorial(n-1)
}

func fetchIP() (string, error) {
	resp, err := http.Get("https://httpbin.org/ip")
	if err != nil {
		return "", err
	}

	defer resp.Body.Close()

	type Response struct {
		Origin string `json:"origin"`
	}
	var response Response

	err = json.NewDecoder(resp.Body).Decode(&response)
	return response.Origin, err
}
